﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using cfg = System.Configuration;
using System.Collections;
using GoldPrinter;
namespace BYManageInfoSystem
{
    public partial class Frm_BatchKaiPiaoHuiLv : Form
    {
        private List<string> lstContractTemp = new List<string>();
        private string m_strFolder = "d:/财务购货合同/";
        private Dictionary<string, int> dicContractToBGCKHTTabID = new Dictionary<string, int>();
        //private List<string> lstContractNotReady = new List<string>();
        //private DGBY.BLL.BGDComeBackTab m_bllBGDComeBackTab = new DGBY.BLL.BGDComeBackTab();
        private readonly DGBY.BLL.BGCKHTTab m_bllBGCKHTTab = new DGBY.BLL.BGCKHTTab();
        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
        public Frm_BatchKaiPiaoHuiLv()
        {
            InitializeComponent();
        }

        private void buttonQuery_Click(object sender, EventArgs e)
        {
            dicContractToBGCKHTTabID.Clear();
            DataSet ds = m_bllBGCKHTTab.ShowAllContractKPHLRecByCondition(@"HxdNoTab.TongJDate >= '" + this.dateTimePickerFrom.Value
                + "'" + " and HxdNoTab.TongJDate <= '" + this.dateTimePickerTo.Value + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    this.listBoxAllContract.Items.Add(dr["合同编号"].ToString());
                    dicContractToBGCKHTTabID.Add(dr["合同编号"].ToString(), Convert.ToInt32(dr["BGCKHTTabID"].ToString(), 10));
                }
            }
        }

        private void buttonGoOne_Click(object sender, EventArgs e)
        {
            lstContractTemp.Clear();
            if (this.listBoxAllContract.SelectedItems.Count >= 0)
            {
                foreach (object o in this.listBoxAllContract.SelectedItems)
                {
                    string strTemp = (string)o;
                    this.listBoxSelected.Items.Add(strTemp);
                    lstContractTemp.Add(strTemp);

                }
                foreach (string o in lstContractTemp)
                {

                    this.listBoxAllContract.Items.Remove(o);
                }
            }
        }

        private void button2GoAll_Click(object sender, EventArgs e)
        {
            int nCount = this.listBoxAllContract.Items.Count;
            for (int i = 0; i < nCount; i++)
            {
                this.listBoxSelected.Items.Add(this.listBoxAllContract.Items[i]);
            }
            for (int i = nCount - 1; i >= 0; i--)
            {
                this.listBoxAllContract.Items.RemoveAt(i);
            }
        }

        private void button3BackOne_Click(object sender, EventArgs e)
        {
            lstContractTemp.Clear();
            if (this.listBoxSelected.SelectedItems.Count >= 0)
            {
                foreach (object o in this.listBoxSelected.SelectedItems)
                {
                    string strTemp = (string)o;
                    this.listBoxAllContract.Items.Add(strTemp);
                    lstContractTemp.Add(strTemp);

                }
                foreach (string o in lstContractTemp)
                {

                    this.listBoxSelected.Items.Remove(o);
                }
            }
        }

        private void button4BackAll_Click(object sender, EventArgs e)
        {
            int nCount = this.listBoxSelected.Items.Count;
            for (int i = 0; i < nCount; i++)
            {
                this.listBoxAllContract.Items.Add(this.listBoxSelected.Items[i]);
            }
            for (int i = nCount - 1; i >= 0; i--)
            {
                this.listBoxSelected.Items.RemoveAt(i);
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            foreach (object o in this.listBoxSelected.Items)
            {
                string strTemp = (string)o;
                int nBGCKHTTabID = dicContractToBGCKHTTabID[strTemp];
                DGBY.Model.BGCKHTTab modelBGCKHTTabOld = m_bllBGCKHTTab.GetModel(nBGCKHTTabID);
                modelBGCKHTTabOld.KaiPiaoHuiLv = Convert.ToDecimal(this.textBoxKaiPiaoHuiLv.Text);
                m_bllBGCKHTTab.Update(modelBGCKHTTabOld);
            }

            MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void ExportExcel(string strContract, ArrayList lstExcelFileName)
        {
            if (Directory.Exists(m_strFolder))//判断是否存在
            {
                //if (File.Exists(strFolder + @"\" + strFin + ".jpg"))
                //    return;

            }
            else
            {
                //Response.Write("不存在，正在创建");
                Directory.CreateDirectory(m_strFolder);//创建新路径
            }
            DataSet ds = m_bllPaidanRegisterTab.GetList("PaidanRegisterTab.ContractCode = '" + strContract.ToString() + "'");
            int nId = 0;
            
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {

                nId = Convert.ToInt32(ds.Tables[0].Rows[0]["PaidanRegisterTabID"].ToString(), 10);
            }
            
            ds = m_bllBGCKHTTab.GetFinBuyGoodsContractList("PaidanRegisterTab.PaidanRegisterTabID = '" + nId.ToString() + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {


            }
            else
            {
             //   MessageBox.Show("该合同号对应的回收报关单未回来!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            DataSet ds2 = m_bllBGCKHTTab.GetFinBuyGoodsContractDetailList("PaidanRegisterTab.PaidanRegisterTabID = '" + nId.ToString() + "'");

            if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
            {


            }
            else
            {
             //   MessageBox.Show("该合同号对应的回收报关单没有明细!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            string strGuiNoLst = "";
            DataSet ds3 = m_bllBGCKHTTab.GetAllCargoBoxInfoEx("PaidanRegisterTab.PaidanRegisterTabID = '" + nId.ToString() + "'");
            if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
            {
                string str = ds3.Tables[0].Rows[0]["集装箱柜号"].ToString();
                string[] arr = str.Split(',');
                string tempa = "";
                int n = 0;
                foreach (string ch in arr)
                {

                    if (ch != "")
                    {
                        if (n == 0)
                            tempa += ch;
                        else
                            tempa += "\n" + ch;
                    }

                    n++;
                }
                strGuiNoLst = tempa;

            }
            DataTable dt = (DataTable)ds2.Tables[0];
            DataTable dt1 = (DataTable)ds.Tables[0];
            //DataSet DsBYCompInfo = bllBYCompanyTab.GetList("CompanyName = '" + dt1.Rows[0]["卖方"].ToString() + "'");
            //DataSet DsSHCompInfo = m_bllSHCompTab.GetList("CompanyName = '" + dt1.Rows[0]["买方"].ToString() + "'");
            if (dt1 != null)
            {

            }
            else
            {
             //   MessageBox.Show("购货合同记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    //SaveFileDialog sfd = new SaveFileDialog();
                    //sfd.Filter = "Excel文件(*.xls)|*.xls";
                    //string outFile = "";

                    //if (sfd.ShowDialog() == DialogResult.OK)
                    //{
                    //    outFile = sfd.FileName;
                    //}
                    //else
                    //{
                    //    return;
                    //}
                    string outFile = m_strFolder + strContract + ".xls";
                    string templetFile = templetFilePath + "财务_购货合同.xlt";
                    lstExcelFileName.Add(outFile);
                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.FinBuyGoodsConstract_DataTableToExcel(dt, dt1, 16, 8, 1, "页", 1, strGuiNoLst, "", false);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
              //  MessageBox.Show("购货合同明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonExport_Click(object sender, EventArgs e)
        {
            ArrayList lstExcelFileName = new ArrayList();
            foreach (object o in this.listBoxSelected.Items)
            {
                string strTemp = (string)o;
                ExportExcel(strTemp, lstExcelFileName);
                //lstExcelFileName.Add(strTemp);
            }
            DateTime dt = System.DateTime.Now;
            string format = "yyyyMMddhhmmss";            // Use this format
            string strTempTime = dt.ToString(format);
            ExcelBase eB = new ExcelBase();
            string outFile = m_strFolder + strTempTime + ".xls";
            eB.GouHuoBooks2Sheets(outFile, lstExcelFileName);
            eB.CloseAll();
        }

        private void Frm_BatchKaiPiaoHuiLv_Load(object sender, EventArgs e)
        {

        }
    }
}
